//
//  ITJiLuTableViewCell.m
//  inrtest
//
//  Created by 张涛 on 2017/4/18.
//  Copyright © 2017年 张涛. All rights reserved.
//附近

#import "ITJiLuTableViewCell.h"

@interface ITJiLuTableViewCell()

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *addressLab;
@property (nonatomic, strong) UILabel *disstanceLab;
@end

@implementation ITJiLuTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *cellId = @"ITJiLuTableViewCell";
    ITJiLuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[ITJiLuTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    return  cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        UIImageView * image = [[UIImageView alloc] init];
        [self.contentView addSubview:image];
        [image setBackgroundColor:[UIColor redColor]];
        self.imageViewV = image;
        
        UILabel *titleLab = [[UILabel alloc]init];
        titleLab.font = DEF_MyFont(DEF_RESIZE_UI(18));
        [self.contentView addSubview:titleLab];
        self.titleLab = titleLab;
        
        UILabel *addressLab = [[UILabel alloc]init];
        addressLab.font = DEF_MyFont(DEF_RESIZE_UI(14));//附近地址的字号
        addressLab.textColor = [UIColor grayColor];
        addressLab.numberOfLines = 0;//显示的行数
        [self.contentView addSubview:addressLab];
        self.addressLab = addressLab;
        
        UILabel *disstanceLab = [[UILabel alloc]init];
        disstanceLab.font = DEF_MyFont(DEF_RESIZE_UI(16));
        disstanceLab.textColor = [UIColor grayColor];
        [self.contentView addSubview:disstanceLab];
        self.disstanceLab = disstanceLab;
        
        UIView *line = [[UIView alloc]init];
        line.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:line];
        
        
        [image mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.left.mas_equalTo(15);
            make.top.mas_equalTo(10);
            make.bottom.mas_equalTo(-10);
//            make.height.mas_equalTo(DEF_RESIZE_UI(45));
            make.width.mas_equalTo(DEF_RESIZE_UI(65));
            
        }];
        
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(image.mas_trailing).with.offset(10);
            make.top.mas_equalTo(DEF_RESIZE_UI(5));
            make.width.mas_lessThanOrEqualTo([UIScreen mainScreen].bounds.size.width - DEF_RESIZE_UI(116));
        }];
        
        [addressLab mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.width.mas_equalTo(DEF_DEVICE_WIDTH-DEF_RESIZE_UI(65)-16-10-10);
            make.height.mas_equalTo(38);
            make.leading.equalTo(image.mas_trailing).with.offset(10);
            make.top.equalTo(self.titleLab.mas_bottom).with.offset(5);
          
        }];
        
        [disstanceLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.contentView.mas_right).offset(DEF_RESIZE_UI(-16));
            make.top.mas_equalTo(titleLab.mas_top);
            make.width.mas_lessThanOrEqualTo(DEF_RESIZE_UI(100));
        }];
        
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(DEF_RESIZE_UI(16));
            make.size.mas_equalTo(CGSizeMake([UIScreen mainScreen].bounds.size.width - DEF_RESIZE_UI(16), 1));
            make.bottom.mas_equalTo(self.contentView.mas_bottom);
        }];
        
    }
    return  self;
}

- (void)setItem:(MKMapItem *)item{
    _item = item;
    
    self.titleLab.text = _item.name;
    self.addressLab.text = [NSString stringWithFormat:@"%@ %@,%@ %@,%@",
                            _item.placemark.country,
                            _item.placemark.administrativeArea,
                            _item.placemark.locality,
                            _item.placemark.subLocality,
                            _item.placemark.thoroughfare];
    
}

- (NSString *)judgeDis: (CGFloat)distance {
    if (distance < 1) {
        NSString *distanceStr = [[NSString stringWithFormat:@"%f", _distance * 1000] substringToIndex:3];
        NSString *str = [NSString stringWithFormat:@"%@m", distanceStr];
        return str;
    }else{
        return @"1km以外";
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
